//
//  YLUserMgr.m
//  DoulinClient
//
//  Created by Apple on 17/3/25.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "YLUserMgr.h"

#define kUserKey @"userKey"

@implementation YLUserMgr

+ (YLUserInfo *)fetchUser {
    @synchronized (self) {
        NSData *udObj = [[NSUserDefaults standardUserDefaults] objectForKey:kUserKey];
        YLUserInfo *user = [NSKeyedUnarchiver unarchiveObjectWithData:udObj];
        return user;
    }
}
+ (void)saveUser:(YLUserInfo *)user {
    @synchronized (self) {
        NSData * udObj = [NSKeyedArchiver archivedDataWithRootObject:user];
        [[NSUserDefaults standardUserDefaults] setObject:udObj forKey:kUserKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
+ (void)deleteUser {
    @synchronized (self) {
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

+ (BOOL)isLogin{
     YLUserInfo *user = [self fetchUser];
     return (user.authorization.intValue > 0);
}

+ (NSString *)getUserID {
    YLUserInfo * user = [self fetchUser];
    if ((user.id == nil) || (user.id.length == 0) || [user.id containsString:@"null"]) {
        return @"";
    } else {
        return user.id;
    }
}
+ (NSString *)getUserName {
    YLUserInfo *user = [self fetchUser];
    if ((user.nickName == nil) || (user.nickName.length == 0) || [user.nickName containsString:@"null"]) {
        return @"";
    } else {
        return user.nickName;
    }
}
+ (NSString *)getenterpriseID {
    YLUserInfo *user = [self fetchUser];
    if ((user.companyId == nil) || (user.companyId.length == 0) || [user.companyId containsString:@"null"]) {
        return @"";
    } else {
        return user.companyId;
    }
}
+ (NSString *)getEnterpriseName {
    YLUserInfo *user = [self fetchUser];
    if ((user.enterpriseName == nil) || (user.enterpriseName.length == 0) || [user.enterpriseName containsString:@"null"]) {
        return @"";
    } else {
        return user.enterpriseName;
    }
}

@end
